const url = require('url');
const got = require('@/utils/got');
const cheerio = require('cheerio');

module.exports = async (ctx) => {
    const web_host = 'http://www.csrc.gov.cn';
    const start_urls = [
        'http://www.csrc.gov.cn/csrc/c100119/common_list.shtml', // 证券市场快报
        'http://www.csrc.gov.cn/csrc/c100120/common_list.shtml', // 证券市场月报
        'http://www.csrc.gov.cn/csrc/c100121/common_list.shtml', // 期货市场周报
        'http://www.csrc.gov.cn/csrc/c100122/common_list.shtml', // 期货市场月报
    ];

    const responses = await Promise.all(start_urls.map(root_url => got({
        method: 'get',
        url: root_url,
    })));

    let list = [];
    responses.forEach(response => {
        const $ = cheerio.load(response.data);
        const lis = $('#list>li');
        for(let i=0; i<lis.length; i++) {
            list.push(lis[i]);
        }
    });

    const count = [];
    for (let i = 0; i < Math.min(list.length, 10); ++i) {
        count.push(i);
    }

    const items = await Promise.all(
        count.map((i) => {
            const item = cheerio.load(list[i])('li');
            const title = item.find('a').first();
            const link = web_host + title.attr('href');

            const pubDate = new Date(item.find('span.date').text().trim() + ' GMT+8').toUTCString();

            return ctx.cache.tryGet('csrc' + link, async () => {
                // const date = item.find('div.postSubtitle').text().trim();
                // const match = /(\d+\/\d+\/\d{4}\s+\d+:\d+:\d+\s*(?:AM|PM))/i.exec(date);
                // const pubDate = match && new Date(match[1] + ' GMT+8').toUTCString();

                const res = await got({ method: 'get',
                    // url: url.resolve(root_url, link)
                    url: link
                });
                const content = cheerio.load(res.data);
                const post = content('div.detail-news');
                post.find('img[data-src]').each((_, ele) => {
                    ele = content(ele);
                    ele.attr('src', ele.attr('data-src'));
                    ele.attr('alt', title.text());
                    ele.attr('title', title.text());
                    ele.removeAttr('class');
                    ele.removeAttr('data-src');
                });

                return {
                    title: title.text(),
                    link,
                    pubDate,
                    description: post.html(),
                };
            });
        })
    );

    ctx.state.data = {
        title: '证券/期货市场快报',
        link: web_host,
        item: items,
    };
};
